import { useEffect,useState } from "react";
// import { clearTimeout } from "timers";
function useFormTime(timepre:number){
    const [formatDate,setFormatDate] = useState<string>('')
    const [timeStr, setTimeStr] = useState<number>(timepre)
 
    
    useEffect(()=>{
       const str =  formatTime(timeStr)
        setFormatDate(str)
        // console.log('formatDate1', timepre);
    }, [timeStr])

    return { formatDate, setTimeStr }
}


export function formatTime(tim: number) {
    const date1 = new Date(tim || new Date().getTime());
    const year = date1.getFullYear()
    const month = date1.getMonth() + 1;
    const day = date1.getDay();
    // console.log(`${year}-${month}-${day}`)
    return `${year}-${month}-${day}`
}


export function debounce(fun,delay){
    let timeId
    return (...args)=>{
        clearTimeout(timeId)
        timeId = setTimeout(
            () => { fun(...args)}
            , delay
        )
    }
}
export default useFormTime